package com.sikadwafastdaisfondturbo.pretloan.adapter

import android.graphics.Typeface
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.sikadwafastdaisfondturbo.lib_baseadapter.SikadwaRecyclerViewAdapter
import com.sikadwafastdaisfondturbo.lib_baseadapter.SikadwaViewHolderHelper
import com.sikadwafastdaisfondturbo.pretloan.R
import com.sikadwafastdaisfondturbo.pretloan.utils.DensityUtils
import com.sikadwafastdaisfondturbo.pretloan.utils.dialog.CommonPopupWindow
import com.sikadwafastdaisfondturbo.pretloan.utils.event.EventType
import com.sikadwafastdaisfondturbo.pretloan.utils.http.HttpHelper
import com.sikadwafastdaisfondturbo.pretloan.utils.rv.LinearDecoration
import org.json.JSONObject

/**
 * Sikadwa
 * @Author wwdeng
 * @CreateTime: 2024/4/18 10:43
 * @Annotation：流程 - 联系人适配器
 */
class ProcessContactInfoAdapter(private val parentView: ViewGroup,recyclerView: RecyclerView?) : SikadwaRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.item_contact_brand) {

    /**
     * 选择联系电话监听
     */
    private var chooseMobileListsner: IChildItemSelectMobileListener? = null
    fun setOnChildItemSelectMobileListener(listener: IChildItemSelectMobileListener){
        chooseMobileListsner = listener
    }

    override fun fillData(helper: SikadwaViewHolderHelper, position: Int, model: JSONObject) {
        var showContactIndex = String.format(mContext.getString(R.string.process_contact_info_index),"${position+1}")
        if (position > 0){
            showContactIndex = "$showContactIndex ${mContext.getString(R.string.process_contact_info_different)}"
        }
        helper.setText(R.id.tvBrandTitle,showContactIndex)

        val recyclerView = helper.getView<RecyclerView>(R.id.recyclerView)
        recyclerView.setHasFixedSize(true)
        recyclerView.isNestedScrollingEnabled = false
        recyclerView.isFocusableInTouchMode = false //不需要焦点
        recyclerView.layoutManager = LinearLayoutManager(mContext)
        if (recyclerView.itemDecorationCount == 0){
            val topDecoration = DensityUtils.dp2px(14f)
            recyclerView.addItemDecoration(LinearDecoration(0,0,0,topDecoration))
        }
        val adapter = ProcessCommonAdapter(recyclerView)
        adapter.typeFaceStyle = Typeface.NORMAL
        recyclerView.adapter = adapter
        recyclerView.itemAnimator = DefaultItemAnimator()
        adapter.setOnItemChildClickListener { parent, childView, childPosition ->
            val childModel = adapter.getItem(childPosition)
            when(childModel.optString("action")){
                "CONTACT_GROUP_RELATION" -> {//选择
                    val dataList = arrayListOf<JSONObject>()
                    val dataArray = childModel.optJSONArray("values")
                    if (dataArray != null){
                        for (i in 0..<dataArray.length()) {
                            dataList.add(dataArray.optJSONObject(i))
                        }
                    }
                    showPopupwindow(childView, dataList) { data ->
                        //选择的Other需要设置为空
                        data.put("otherValue","")
                        childModel.put("selectItem", data)
                        adapter.notifyItemChanged(childPosition)

                        val subRemark = "${data.optString("value")}$$${data.optString("duration")}"
                        HttpHelper.addEvent(String.format(EventType.CLICK.CLICK_STEP_XX_SELECT, "${model.optString("stepTitle")}_${childModel.optString("paramName")}_${position+1}"), subRemark)
                    }
                }
                "CONTACT_GROUP_PHONE" -> {//联系电话
                    if (chooseMobileListsner != null){
                        chooseMobileListsner?.onChildSelectMobileClick(position,childModel)
                    }
                }
            }
        }

        val dataArray = model.optJSONArray("dataList")
        val dataList = arrayListOf<JSONObject>()
        if (dataArray != null){
            for (i in 0..<dataArray.length()){
                val contactItem = dataArray.optJSONObject(i)
                contactItem.put("stepTitle",model.optString("stepTitle"))
                contactItem.put("stepType","CONTACT")
                contactItem.put("stepItemIndex",position+1)
                dataList.add(contactItem)
            }
        }
        adapter.data = dataList
    }

    /**
     * choose Mobile
     */
    interface IChildItemSelectMobileListener{
        fun onChildSelectMobileClick(parentPosition: Int,childModel: JSONObject)
    }

    /**
     * SELECT
     */
    fun interface IChildItemSelectListener{
        fun onChildSelectClick(data: JSONObject)
    }

    /**
     * 选择选项
     */
    private fun showPopupwindow(view: View, dataList: ArrayList<JSONObject>, listener: IChildItemSelectListener){
        val selectPopup = CommonPopupWindow(mContext)
        selectPopup.setWindowWidth(view.width + DensityUtils.dp2px(10f))
        selectPopup.setSelectItemListener(object : CommonPopupWindow.IItemClickListener {
            //完成
            override fun onConfirmAction(data: JSONObject?) {
                data?.let {
                    listener.onChildSelectClick(it)
                }
            }
            //关闭监听
            override fun onDismissListener() {
            }
        })
        selectPopup.show(view, parentView)
        selectPopup.setBrandList(dataList)
    }
}